package com.hmall.item.web;

import com.hmall.common.dto.ItemDTO;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("item")
@Slf4j
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> page(@RequestParam("page") Integer page, @RequestParam("size") Integer size){
        PageDTO<Item> pageDTO = itemService.getAll(page,size);
       return  pageDTO;
    }

    /**
     * 新增商品
     * @param item
     */
    @PostMapping
    public void insert(@RequestBody Item item){
        itemService.insert(item);
    }

    /**\
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item getId(@PathVariable Integer id){
        return itemService.getId(id);
    }

    /**
     * 修改商品
     * @param item
     */
    @PutMapping
    public void update(@RequestBody Item item){
        itemService.updateById(item);
    }

    /**
     * 修改商品状态
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void changeStatus(@PathVariable Integer id,@PathVariable Integer status){
        Item id1 = itemService.getId(id);
        id1.setStatus(status);
        itemService.updateById(id1);
    }
    @DeleteMapping("/{id}")
    public  void delete(@PathVariable Integer id){
        itemService.delete(id);
    }
}
